/* Code based on the one available here: http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet */

(function() {
  var el=window.document.createElement('div'),
   b=window.document.getElementsByTagName('body')[0];
  var otherlib=false,
   msg='';
  el.style.position='fixed';
  el.style.height='32px';
  el.style.width='220px';
  el.style.marginLeft='-110px';
  el.style.top='0';
  el.style.left='50%';
  el.style.padding='5px 10px';
  el.style.zIndex = 1001;
  el.style.fontSize='12px';
  el.style.color='#222';
  el.style.backgroundColor='#f99';
  b.appendChild(el);
  if(typeof jQuery!='undefined') {
    msg='This page already using jQuery v'+jQuery.fn.jquery;
    return showMsg(msg);
  } else if (typeof $=='function') {
    otherlib=true;
  }
  // more or less stolen form jquery core and adapted by paul irish
function getScript(url,success){
  var script=window.document.createElement('script');
  script.src=url;
  var head=window.document.getElementsByTagName('head')[0],
  done=false;
  // Attach handlers for all browsers
  script.onload=script.onreadystatechange = function(){
    if ( !done && (!this.readyState
        || this.readyState == 'loaded'
        || this.readyState == 'complete') ) {
        done=true;
        success();
        script.onload = script.onreadystatechange = null;
        head.removeChild(script);
    }
  };
  head.appendChild(script);
}

getScript('http://code.jquery.com/jquery-1.5.2.min.js',function() {
  var msg='';
  if (typeof jQuery=='undefined') {
    msg='Sorry, but jQuery wasn\'t able to load';
  } else {
    msg='This page is now jQuerified with v' + jQuery.fn.jquery;
    if (otherlib) {msg+=' and noConflict(). Use $jq(), not $().';}
  }
  return showMsg(msg);
});

getScript('http://wikignpi.googlecode.com/hg/xregexp-min.js',function() {
  var msg='';
  if (typeof XRegExp=='undefined') {
    msg='Sorry, but XRegExp wasn\'t able to load';
  } else {
    msg='This page is now XRegExp\'ied.';
  }
  return showMsg(msg);
});

getScript('http://wikignpi.googlecode.com/hg/GoogleCodeWikiParser.min.js',function() {
  var msg='';
  if (typeof GoogleCodeWikiParser=='undefined') {
    msg='Sorry, but GoogleCodeWikiParser wasn\'t able to load';
  } else {
    msg='This page is now GoogleCodeWikiParser\'ied.';
  }
  return showMsg(msg);
});

window.setTimeout(moover,500);

function showMsg(msg) {
  el.innerHTML=msg;
  window.setTimeout(function() {
    if (typeof jQuery=='undefined') {
      //b.removeChild(el);
      el.innerHTML='';
    } else {
      jQuery(el).fadeOut('slow',function() {
        //jQuery(this).remove();
        jQuery(this).html('');
      });
      if (otherlib) {
        $jq=jQuery.noConflict();
      }
    }
  } ,500);
};

//var gcwp, gnpicmt_txtarea, gnpicmt_cmtDiv;

function updateView() {
  var c = gnpicmt_txtarea.attr('value');
  try {
    c = gcwp.parse(c)
    gnpicmt_cmtDiv.html(c);
  }
  catch(e) {
    alert("Exception while parsing wiki code:\n"+e);
  }
}

function moover(){
  var tryagain=true;
  if(typeof jQuery!='undefined' && typeof GoogleCodeWikiParser!='undefined' && typeof XRegExp!='undefined'){
    if(jQuery('textarea[name=content]').length>0 && jQuery('div#commentform').length>0)
    {
      var divCmtForm = jQuery('div#commentform');
      gnpicmt_txtarea = jQuery('textarea[name=content]');

      gnpicmt_cmtDiv = jQuery('<div>').addClass('commentcontent');
      divCmtForm.prepend(gnpicmt_cmtDiv);
      divCmtForm.prepend('<p><strong>Preview</strong> comment with <a rel="nofollow" href="http://code.google.com/p/wikiwym/" target="_blank">wikiwym</a>/<a rel="nofollow" href="http://code.google.com/p/wikignpi/" target="_blank">wikignpi</a>:</p>');

      gnpicmt_cmtDiv.html('');

      jQuery(document).ready(function() {
        jQuery.noConflict();
        gcwp = new GoogleCodeWikiParser();
        updateView();
        gnpicmt_txtarea.keypress(function(event) {
          if (event.keyCode == 13) {
            updateView();
          }
        });
      });

      tryagain=false;
    }
  }
  if(tryagain) window.setTimeout(moover,500);
};

})();


